//
//  JgAdsNewSuspensionViewController.m
//  JgAds-Demo
//
//  Created by 陈则富 on 2021/9/13.
//

#import "JgAdsNewSuspensionViewController.h"
#import <ADJgKit/ADJgKitMacros.h>
#import <JgAdsSDK/JgAdsSuspensionAdView.h>
#import "UIView+Toast.h"


@interface JgAdsNewSuspensionViewController()<JgAdsSuspensionAdViewDelegate>

@property (nonatomic, assign) JgAdsSuspensionType suspensionType;

@property (nonatomic ,strong) JgAdsSuspensionAdView *suspensionAdView;

@end

@implementation JgAdsNewSuspensionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.suspensionType = JgAdsSuspensionTypeProfile;
    
    UIButton *setAdConfigBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [setAdConfigBtn setTitle:@"切换" forState:(UIControlStateNormal)];
    [setAdConfigBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    setAdConfigBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    setAdConfigBtn.frame = CGRectMake(0, 0, 50, 20);
    [setAdConfigBtn addTarget:self action:@selector(showTypeSelect) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:setAdConfigBtn];
    self.navigationItem.rightBarButtonItem = rightItem;

    [self loadSuspension];
}

- (void)loadSuspension {
    [self.suspensionAdView removeFromSuperview];
    self.suspensionAdView = nil;
    
    CGSize adSize;
    if (_suspensionType == JgAdsSuspensionTypeProfile) {
        adSize = CGSizeMake(243, 28);
    } else if (_suspensionType == JgAdsSuspensionTypeSingleText) {
        adSize = CGSizeMake(260, 34);
    } else {// _suspensionType == JgAdsSuspensionTypeMultipleText
        adSize = CGSizeMake(83, 127);
    }
    self.suspensionAdView = [[JgAdsSuspensionAdView alloc] initWithFrame:CGRectMake((kADJGScreenWidth-adSize.width)/2, 250, adSize.width, adSize.height) posId:@"475e32806fda" suspensionType:self.suspensionType];
    self.suspensionAdView.delegate = self;
    self.suspensionAdView.isAutoPlay = NO;
    self.suspensionAdView.viewController = self;
    [self.suspensionAdView loadRequest];
    [self.view addSubview:self.suspensionAdView];
}

#pragma mark - JgAdsSuspensionAdViewDelegate

/**
 *  请求成功
 */
- (void)jgadsSuspensionSuccessLoad:(JgAdsSuspensionAdView *)jgadsSuspensionView {
    NSLog(@"%s，创新广告加载成功",__func__);
}

/**
 *  请求失败
 */
- (void)jgadsSuspensionViewFailedToLoadWithError:(NSError *)error {
    NSLog(@"创新广告加载失败%@",error);
}

/**
 *  展示回调
 */
- (void)jgadsSuspensionViewDidPresent:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，创新广告展示成功",__func__);
}

/**
 *  曝光回调
 */
- (void)jgadsSuspensionViewWillExpose:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，创新广告曝光",__func__);
}


/**
 *  点击回调
 */
- (void)jgadsSuspensionViewClicked:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，创新广告点击",__func__);
}


/**
 *  被用户关闭时调用
 */
- (void)jgadsSuspensionViewWillClose:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，创新广告关闭",__func__);
    [_suspensionAdView removeFromSuperview];
    _suspensionAdView = nil;
}

/**
 *  被用户关闭广告落地页调用
 */
- (void)jgadsSuspensionViewCloseLandingPage:(JgAdsSuspensionAdView *)jgadsSuspensionView{
    NSLog(@"%s，创新广告关闭落地页",__func__);
}

#pragma mark - other

- (void)showTypeSelect {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"" message:@"选择创新广告类型" preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *profileType = [UIAlertAction actionWithTitle:@"头像广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = JgAdsSuspensionTypeProfile;
        [self loadSuspension];
    }];
    UIAlertAction *singleTextType = [UIAlertAction actionWithTitle:@"单行文字链广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = JgAdsSuspensionTypeSingleText;
        [self loadSuspension];
    }];
    UIAlertAction *multipleTextType = [UIAlertAction actionWithTitle:@"多行文字链广告" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        self.suspensionType = JgAdsSuspensionTypeMultipleText;
        [self loadSuspension];
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [alertVc addAction:profileType];
    [alertVc addAction:singleTextType];
    [alertVc addAction:multipleTextType];
    [alertVc addAction:cancle];
    [self presentViewController:alertVc animated:YES completion:nil];
}

@end
